/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SubsistemaPaciente.Derivacion;

import clienteSandraParada.Login;
import clienteSandraParada.Mensajeria;
import java.awt.Color;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;

/**
 *
 * @author hernan
 */
public class DialogoInfoDerivacion extends javax.swing.JDialog {

    private Derivacion der;
    private Long idDerivacion;
    
    public DialogoInfoDerivacion(java.awt.Frame parent, boolean modal, Derivacion der, Long id) {
        super(parent, modal);
        initComponents();
        this.setIconImage(new ImageIcon(getClass().getResource("/GUI/Img/logo2.png")).getImage());
        this.der = der;
        this.idDerivacion = id;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel3 = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        campoRutPaciente = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        comboBoxPaciente = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        campoRutProfesional = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        comboBoxProfesional = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        dateChooserFecha = new com.toedter.calendar.JDateChooser();
        jLabel3 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        textAreaDescripcion = new javax.swing.JTextArea();
        jLabel14 = new javax.swing.JLabel();
        botonAceptar = new javax.swing.JButton();
        botonCancelar = new javax.swing.JButton();
        mensajeriaInformacion = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos paciente"));

        jLabel9.setText("Nombre:");

        jLabel10.setText("Rut:");

        campoRutPaciente.setEditable(false);

        jLabel5.setText("*");

        comboBoxPaciente.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Seleccionar" }));
        comboBoxPaciente.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                comboBoxPacienteItemStateChanged(evt);
            }
        });

        jLabel7.setText("*");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel9, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(campoRutPaciente, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(comboBoxPaciente, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel7)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(comboBoxPaciente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(campoRutPaciente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos profesional"));

        jLabel11.setText("Nombre:");

        jLabel12.setText("Rut:");

        campoRutProfesional.setEditable(false);

        jLabel15.setText("*");

        comboBoxProfesional.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Seleccionar" }));
        comboBoxProfesional.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                comboBoxProfesionalItemStateChanged(evt);
            }
        });

        jLabel6.setText("*");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel12, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel11, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(campoRutProfesional, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel15, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(comboBoxProfesional, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel6)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(comboBoxProfesional, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(campoRutProfesional, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12)
                    .addComponent(jLabel15))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos derivación"));

        jLabel3.setText("Fecha:");

        jLabel1.setText("Descripción:");

        textAreaDescripcion.setColumns(20);
        textAreaDescripcion.setRows(5);
        jScrollPane1.setViewportView(textAreaDescripcion);

        jLabel14.setText("*");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(dateChooserFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel14, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(70, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(dateChooserFecha, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel14, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addComponent(jLabel1)))
                .addContainerGap(25, Short.MAX_VALUE))
        );

        botonAceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/Img/Iconos16X16/Aceptar.png"))); // NOI18N
        botonAceptar.setText("Aceptar");
        botonAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonAceptarActionPerformed(evt);
            }
        });

        botonCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/Img/Iconos16X16/Cancelar.png"))); // NOI18N
        botonCancelar.setText("Cancelar");
        botonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCancelarActionPerformed(evt);
            }
        });

        mensajeriaInformacion.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        mensajeriaInformacion.setText("Área de mensajería");

        jLabel4.setText("* Campos obligatorios");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(botonAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(botonCancelar)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(mensajeriaInformacion, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(botonCancelar)
                        .addComponent(botonAceptar)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
                .addComponent(mensajeriaInformacion)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void botonAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonAceptarActionPerformed
        this.normalizarMensajeria();
        if (camposObligatoriosLlenos()) {
            this.dispose();
            service.Paciente paciente = (service.Paciente)cargarPaciente(campoRutPaciente.getText());
            service.Usuario us = (service.Usuario)cargarUsuario(campoRutProfesional.getText());
            if(us!=null && paciente !=null){
                if (botonAceptar.getText().equalsIgnoreCase("Agregar")) {
                    String rutPaciente = paciente.getRut();
                    String nombresPaciente = paciente.getNombres();
                    String apPatPac = paciente.getApellidoPaterno();
                    String apMatPac = paciente.getApellidoMaterno();
                    String rutProf = us.getRut();
                    String nombresProf = us.getNombres();
                    String apPatProf = us.getApellidoPaterno();
                    String apMatProf = us.getApellidoMaterno();
                    String fecha = capturarFecha();
                    String descripcion = textAreaDescripcion.getText();
                    agregarDerivacion(apPatPac, apMatPac, apPatProf, apMatProf, descripcion, fecha, nombresPaciente, nombresProf, rutPaciente, rutProf, true);
                    String nuevo = llenarString(rutPaciente, nombresPaciente, apPatPac, apMatPac, rutProf, nombresProf, apPatProf, apMatProf, fecha, descripcion);
                    registrar(Login.getUser(), "Derivación", "Agregó a profesional:" +rutProf+"de paciente :"+rutPaciente, "No Aplica", nuevo);
                }

                else if(botonAceptar.getText().equalsIgnoreCase("Guardar")){
                    service.Derivacion sd = (service.Derivacion)cargarDerivacion(idDerivacion);
                    String antiguo = llenarString(sd);
                    modificarDer(sd, paciente, us);
                    String nuevo = llenarString(sd);
                    registrar(Login.getUser(), "Derivación", "Modificó a profesional:" +sd.getRutProfesional()+"de paciente :"+sd.getRutPaciente(), antiguo, nuevo);
                }
                der.actualizarTabla();                
            }                       
        }
        else {
            this.cambiarEtiquetaMensajeria(Mensajeria.mensajeCamposObligatorios(), Color.red);
        }
    }//GEN-LAST:event_botonAceptarActionPerformed

    private void botonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_botonCancelarActionPerformed

    private void comboBoxProfesionalItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_comboBoxProfesionalItemStateChanged
        String p = this.comboBoxProfesional.getSelectedItem().toString();
        String rut = "";
        if (p.equals("Seleccionar")) {
            this.campoRutProfesional.setText("");
        }
        java.util.List<service.Usuario> usuarios = listarUsuarios();
        for (service.Usuario u : usuarios) {
            if (u.getTipoCuenta().equals("Profesional")) {
                String profesional = u.getNombres() + " " + u.getApellidoPaterno() + " " + u.getApellidoMaterno();
                if (profesional.equals(p)) {
                    rut = u.getRut();
                    this.campoRutProfesional.setText(rut);

                }
            }
        }
        if (!comboBoxProfesional.getSelectedItem().equals("Seleccionar") && comboBoxPaciente.getSelectedItem().equals("Seleccionar")) {
            agregarItemPaciente(listarEnlaceProfesionalPacientes(), rut);
        }
    }//GEN-LAST:event_comboBoxProfesionalItemStateChanged

    public void agregarItemPaciente(java.util.List<service.EnlaceProfesionalPaciente> enlaces, String rutProfesional) {

        comboBoxPaciente.removeAllItems();
        comboBoxPaciente.addItem("Seleccionar");
        for (service.EnlaceProfesionalPaciente e : enlaces) {
            if (e.getRutProfesional().equals(rutProfesional)) {
                service.Paciente paciente = (service.Paciente) cargarPaciente(e.getRutPaciente());
                comboBoxPaciente.addItem(paciente.getNombres() + " " + paciente.getApellidoPaterno() + " " + paciente.getApellidoMaterno());
            }
        }
    }
    
    private void comboBoxPacienteItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_comboBoxPacienteItemStateChanged
        java.util.List<service.Paciente> lista = (java.util.List<service.Paciente>)listarPacientes();
        for(service.Paciente paciente : lista){
            String nombre = paciente.getNombres()+" "+paciente.getApellidoPaterno()+" "+paciente.getApellidoMaterno();
            if(nombre.equalsIgnoreCase((String)evt.getItem())){
                campoRutPaciente.setText(paciente.getRut());
                break;
            }
            else{
                campoRutPaciente.setText("");
            }
        }
    }//GEN-LAST:event_comboBoxPacienteItemStateChanged
    
    public void cambiarEtiquetaMensajeria(String mensaje, Color color){
        mensajeriaInformacion.setText(mensaje);
        mensajeriaInformacion.setForeground(color);
    }
        
    public boolean camposObligatoriosLlenos(){
        boolean b = false;
        if(
           comboValido(comboBoxProfesional) &&
           comboValido(comboBoxPaciente) &&
           !campoRutProfesional.getText().equals("") &&
           !campoRutPaciente.getText().equals("") &&
           dateChooserFecha.getDate() != null)
        {
           b=true;
        }
        return b;
    }
    
    public boolean comboValido(JComboBox combo){
        Boolean b;
        if(combo.getSelectedItem() != null){
            if(combo.getSelectedItem().equals("") || combo.getSelectedItem().equals("Seleccionar")){
                b = false;
            }
            else{
                b = true;
            }
        }
        else{
            b = false;
        }
        return b;
    }
    
    public String capturarFecha(){
        Calendar cal = Calendar.getInstance();
        String fecha="dd-MM-yyyy";
        if(this.dateChooserFecha.getDate()!=null){
            cal.setTime(this.dateChooserFecha.getDate());
            //Dia - mes - año
            fecha= Integer.toString(cal.get(Calendar.DAY_OF_MONTH)) + "-"
                    + Integer.toString(cal.get(Calendar.MONTH) + 1) + "-"
                    + Integer.toString(cal.get(Calendar.YEAR));
       }
       return fecha;
    }
    
    public void cargarUsuarios(){
        java.util.List<service.Usuario> lista = (java.util.List<service.Usuario>)listarUsuarios();
        for(service.Usuario us : lista){
            String nombre = us.getNombres()+" "+us.getApellidoPaterno()+" "+us.getApellidoMaterno();
            comboBoxProfesional.addItem(nombre);
        }
    }
    
    public void cargarPacientes(){
        java.util.List<service.Paciente> lista = (java.util.List<service.Paciente>)listarPacientes();
        for(service.Paciente paciente : lista){
            String nombre = paciente.getNombres()+" "+paciente.getApellidoPaterno()+" "+paciente.getApellidoMaterno();
            comboBoxPaciente.addItem(nombre);
        }
    }
        
    public JButton getBotonAceptar(){
        return this.botonAceptar;
    }
                           
    public void limpiarCampos(){
        comboBoxProfesional.setSelectedItem((Object)"Seleccionar");
        comboBoxPaciente.setSelectedItem((Object)"Seleccionar");
        campoRutProfesional.setText("");
        campoRutPaciente.setText("");
        dateChooserFecha.setDate(null);
        textAreaDescripcion.setText("");
        normalizarMensajeria();
    }
    
    public void llenarCampos(service.Derivacion der){      
        limpiarCampos();
        String nombrePaciente = der.getNombresPaciente()+" "+der.getApellidoPaternoPaciente()+" "+der.getApellidoMaternoPaciente();
        String nombreProfesional = der.getNombresProfesional()+" "+der.getApellidoPaternoProfesional()+" "+der.getApellidoMaternoProfesional();
       
        idDerivacion = der.getId();
        comboBoxProfesional.setSelectedItem((Object)nombreProfesional);
        comboBoxPaciente.setSelectedItem((Object)nombrePaciente);
        campoRutProfesional.setText(der.getRutProfesional());
        campoRutPaciente.setText(der.getRutPaciente());
        if(!der.getFecha().equals("dd-MM-yyyy")){
            this.dateChooserFecha.setDate(stringToDate(der.getFecha()));
        }
        textAreaDescripcion.setText(der.getDescripcion());
    }  
    
    public String llenarString(service.Derivacion der){
        String nombrePac = der.getNombresPaciente()+" "+der.getApellidoPaternoPaciente()+" "+der.getApellidoMaternoPaciente();
        String nombreProf = der.getNombresProfesional()+" "+der.getApellidoPaternoProfesional()+" "+der.getApellidoMaternoProfesional();
        String s = "Rut paciente: "+ der.getRutPaciente()+"\n"+
            "Nombre paciente: "+ nombrePac+"\n"+
            "Rut profesional: "+ der.getRutProfesional()+"\n"+
            "Nombre profesional: "+ nombreProf+"\n"+
            "Fecha: "+ der.getFecha()+"\n"+
            "Descripción: "+ der.getDescripcion();
    return s;
    }
    
    public String llenarString(String rutPac, String nombresPac, String apPatPac, String apMatPac, String rutProf, String nombresProf, String apPatProf, String apMatProf, String fecha, String desc){
        String nombrePac = nombresPac+" "+apPatPac+" "+apMatPac;
        String nombreProf = nombresProf+" "+apPatProf+" "+apMatProf;
        String s = "Rut paciente: "+ rutPac +"\n"+
                "Nombre paciente: "+ nombrePac +"\n"+
                "Rut profesional: "+ rutProf +"\n"+
                "Nombre profesional: "+ nombreProf +"\n"+
                "Fecha: "+ fecha +"\n"+
                "Descripción: "+ desc;
        return s;
    }
    
    private void modificarDer(service.Derivacion sp, service.Paciente paciente, service.Usuario us) {
        sp.setNombresProfesional(us.getNombres());
        sp.setApellidoPaternoProfesional(us.getApellidoPaterno());
        sp.setApellidoMaternoProfesional(us.getApellidoMaterno());
        sp.setRutProfesional(us.getRut());

        sp.setNombresPaciente(paciente.getNombres());
        sp.setApellidoPaternoPaciente(paciente.getApellidoPaterno());       
        sp.setApellidoMaternoPaciente(paciente.getApellidoMaterno());
        sp.setRutPaciente(paciente.getRut());
        
        sp.setDescripcion(textAreaDescripcion.getText());
        sp.setFecha(capturarFecha());       
        modificarDerivacion(sp);
    }
    
    public void normalizarMensajeria(){
        mensajeriaInformacion.setText("Área de mensajería");
        mensajeriaInformacion.setForeground(Color.black);
    }
            
    public Date stringToDate(String strFecha){
        SimpleDateFormat formatoDelTexto = new SimpleDateFormat("dd-MM-yyyy");
        Date fecha = null;
        try {
            fecha = formatoDelTexto.parse(strFecha);
        } catch (ParseException ex) {
            ex.printStackTrace();
        }
        return fecha;
    }

    public String validarCombo(JComboBox combo) {
        if(comboValido(combo)){
            return combo.getSelectedItem().toString();
        }
        return "";
    } 
        
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonAceptar;
    private javax.swing.JButton botonCancelar;
    private javax.swing.JTextField campoRutPaciente;
    private javax.swing.JTextField campoRutProfesional;
    private javax.swing.JComboBox comboBoxPaciente;
    private javax.swing.JComboBox comboBoxProfesional;
    private com.toedter.calendar.JDateChooser dateChooserFecha;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel mensajeriaInformacion;
    private javax.swing.JTextArea textAreaDescripcion;
    // End of variables declaration//GEN-END:variables
    
    private static boolean agregarDerivacion(java.lang.String apellidoPaternoPaciente, java.lang.String apellidoMaternoPaciente, java.lang.String apellidoPaternoProfesional, java.lang.String apellidoMaternoProfesional, java.lang.String descripcion, java.lang.String fecha, java.lang.String nombresPaciente, java.lang.String nombresPofesional, java.lang.String rutPaciente, java.lang.String rutProfesional, boolean visible) {
        service.DerivacionWS_Service service = new service.DerivacionWS_Service();
        service.DerivacionWS port = service.getDerivacionWSPort();
        return port.agregarDerivacion(apellidoPaternoPaciente, apellidoMaternoPaciente, apellidoPaternoProfesional, apellidoMaternoProfesional, descripcion, fecha, nombresPaciente, nombresPofesional, rutPaciente, rutProfesional, visible);
    }
    
    private static Object cargarDerivacion(java.lang.Long id) {
        service.DerivacionWS_Service service = new service.DerivacionWS_Service();
        service.DerivacionWS port = service.getDerivacionWSPort();
        return port.cargarDerivacion(id);
    }

    private static Object cargarPaciente(java.lang.String rut) {
        service.PacienteWS_Service service = new service.PacienteWS_Service();
        service.PacienteWS port = service.getPacienteWSPort();
        return port.cargarPaciente(rut);
    }
    
    private static Object cargarUsuario(java.lang.String rut) {
        service.UsuarioWSService service = new service.UsuarioWSService();
        service.UsuarioWS port = service.getUsuarioWSPort();
        return port.cargarUsuario(rut);
    }
    
    private static boolean modificarDerivacion(service.Derivacion arg0) {
        service.DerivacionWS_Service service = new service.DerivacionWS_Service();
        service.DerivacionWS port = service.getDerivacionWSPort();
        return port.modificarDerivacion(arg0);
    }

    private static Long registrar(service.Usuario usuario, java.lang.String modulo, java.lang.String accion, java.lang.String valorAnterior, java.lang.String valorNuevo) {
        service.BitacoraWSService service = new service.BitacoraWSService();
        service.BitacoraWS port = service.getBitacoraWSPort();
        return port.registrar(usuario, modulo, accion, valorAnterior, valorNuevo);
    }

    private static java.util.List<service.Usuario> listarUsuarios() {
        service.UsuarioWSService service = new service.UsuarioWSService();
        service.UsuarioWS port = service.getUsuarioWSPort();
        return port.listarUsuarios();
    }

    private static java.util.List<service.Paciente> listarPacientes() {
        service.PacienteWS_Service service = new service.PacienteWS_Service();
        service.PacienteWS port = service.getPacienteWSPort();
        return port.listarPacientes();
    }
    
    private static java.util.List<service.EnlaceProfesionalPaciente> listarEnlaceProfesionalPacientes() {
        service.EnlaceProfesionalPacienteWSService service = new service.EnlaceProfesionalPacienteWSService();
        service.EnlaceProfesionalPacienteWS port = service.getEnlaceProfesionalPacienteWSPort();
        return port.listarEnlaceProfesionalPacientes();
    }
    
}

